$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'erp/pimoinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'pimoInfoId', name: 'pimoInfoId', index: 'pimo_info_id', width: 50, key: true,hidden:true },
			{ label: '啤模编码', name: 'pimoCode', index: 'pimo_code', width: 80 }, 			
			{ label: '啤模名称', name: 'pimoName', index: 'pimo_name', width: 80 }, 			
			{ label: '客户编码', name: 'customerCode', index: 'customer_code', width: 80 }, 			
			{ label: '客户名称', name: 'customerName', index: 'customer_name', width: 80 }, 			
			{ label: '纸质', name: 'paperInfoId', index: 'paper_info_id', width: 80 }, 			
			{ label: '货号', name: 'goodsCode', index: 'goods_code', width: 80 }, 			
			{ label: '用料长', name: 'usePaperLong', index: 'use_paper_long', width: 80 }, 			
			{ label: '用料宽', name: 'usePaperWeigth', index: 'use_paper_weigth', width: 80 }, 			
			{ label: '单位', name: 'unitType', index: 'unit_type', width: 80 }, 			
			{ label: '一模成型数量', name: 'pimoCount', index: 'pimo_count', width: 80 }, 			
			{ label: '每箱用料比例', name: 'useRate', index: 'use_rate', width: 80 }, 			
			{ label: '存放位置', name: 'letPlace', index: 'let_place', width: 80 }, 			
			{ label: '组件编号', name: 'commonCode', index: 'common_code', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		pimoInfo: {},
		customerCode: '',
		customerList: [],
		customerVO: {},
		paperInfoList: [],
		unitTypeOptions: [],
		unitType: ''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.pimoInfo = {};
		},
		update: function (event) {
			var pimoInfoId = getSelectedRow();
			if(pimoInfoId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(pimoInfoId)
		},
		saveOrUpdate: function (event) {
			vm.pimoInfo.unitType=vm.unitType;
			vm.pimoInfo.customerCode=vm.customerCode;
			var url = vm.pimoInfo.pimoInfoId == null ? "erp/pimoinfo/save" : "erp/pimoinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.pimoInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var pimoInfoIds = getSelectedRows();
			if(pimoInfoIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "erp/pimoinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(pimoInfoIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(pimoInfoId){
			$.get(baseURL + "erp/pimoinfo/info/"+pimoInfoId, function(r){
                vm.pimoInfo = r.pimoInfo;
                vm.customerCode = r.pimoInfo.customerCode;
                vm.unitType=r.pimoInfo.unitType;
                
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		getCustomerList: function(){
			$.get(baseURL + "erp/customerinfo/list", 
				{
		            page:{"currPage":1,"limit":10}
		        },
		        function(r){
	                if(r.code==0){
	                	vm.customerList=r.page.list;
	                }
	            }
	        );
		},
		changeCustomer:function(){
			for(var i=0;i<this.customerList.length;i++){
				if(this.customerCode==this.customerList[i].customerCode){
					this.customerVO=this.customerList[i];
					this.pimoInfo.customerName=this.customerList[i].customerName;
					break;
				}
			}
		},
		getPaperInfoList: function(){
			$.get(baseURL + "erp/paperinfo/list", 
				{
		            page:{"currPage":1,"limit":10}
		        },
		        function(r){
	                if(r.code==0){
	                	vm.paperInfoList=r.page.list;
	                }
	            }
	        );
		},
		getUnitInfoList: function(){
			$.get(baseURL + "erp/baseparam/findInfoByCode", 
					{
						"paramCode":"UNIT_TYPE"
					},
			        function(r){
		                if(r.code==0){
		                	vm.unitTypeOptions=r.list;
		                }
		            }
		        );
		},
		changeUnit:function(){
			debugger;
			for(var i=0;i<this.unitTypeOptions.length;i++){
				if(this.unitType==this.unitTypeOptions[i].paramValue){
					this.pimoInfo.unitValue=this.unitTypeOptions[i].attr1;
					break;
				}
			}
		},
	},
	mounted() {
	      this.getCustomerList();
	      this.getPaperInfoList();
	      this.getUnitInfoList();
	      
	}
});